package cn.biplam.front.component.view.mdManage.buffer2md.main;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabCode;
import cn.biplam.common.paramters.ModelImportParam.Operator_Type;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.PagingComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript;

public class Buffer2MdTableOpt extends AjxResponse {

	public static final String cId="2207";
	/** 略过*/
	public static final String aId_ignore="220701";
	/** 更新*/
	public static final String aId_update="220702";
	/** 删除*/
	public static final String aId_delete="220703";
	
	/** 操作类型*/
	private String optType; 
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(aId_ignore.equals(actionId)){
			optType = Operator_Type.Ignore.toString();
		}else if(aId_update.equals(actionId)){
			optType = Operator_Type.Update.toString();
		}else if(aId_delete.equals(actionId)){
			optType = Operator_Type.Delete.toString();
		}
		return this.get();
	}
	
	
	private HtmlWindow get() throws ErrorMessage{
		HtmlWindow page = new HtmlDiv();
		/**页码*/
		String pageNum = this.params.getByKey("pageNum");
		/**行id(compare_table的id)*/
		String rowid = this.recordId;
		/**任务号*/
		String taskId = this.params.getByKey("taskId");
		
		/** 更新操作类型*/
		this.updateOperatorType(rowid, optType);
		/** 绑定刷新脚本*/
		this.bindScript(page, pageNum, taskId);
		
		
		return page;		
	}
	
	/**
	 * 更新操作类型
	 * @param ctid
	 * @param operatorType
	 * @throws ErrorMessage
	 */
	private void updateOperatorType(String ctid,String operatorType) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(MdStruc.ImpCompTabStruc.CTID, ctid);
		filter.put(MdStruc.ImpCompTabStruc.OPERATOR_TYPE, operatorType);
		Operator.get(BufCompTabCode.Update,mUser.getUserID(), filter);
	}
	
	/**
	 * 绑定刷新脚本
	 * @param page
	 * @param pageNum
	 * @param rowid
	 * @throws ErrorMessage
	 */
	private void bindScript(HtmlWindow page,String pageNum,String taskId) throws ErrorMessage{
		//附加提交参数为当前页数
		KvMap<String, String> submitParams = new KvMap<String, String>();
		submitParams.put(PagingComp.baseID+Buffer2MdCompareTable.cId+"pageNum", 
				pageNum);
		//生成提交脚本,提交完后刷新当前页面
		String script = AjxScript.getSpParams(Buffer2MdCompareTable.cId, //提交容器ID
				Buffer2MdCompareTable.aId,                                   //提交动作ID
				Buffer2MdCompareTable.cId,                                   //父容器ID
				taskId,                                                      //主键ID
				submitParams                                                 //提交附加参数
				);
		page.bind(script);
	}

}
